package com.dy.security.controller;

import com.dy.security.dto.UserDTO;
import com.dy.security.entity.UserEntity;
import com.dy.security.service.UserService;
import com.dy.sucetity.utils.ApiResousUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: ding-yu
 * @Date: 2022/5/24 14:23
 * @Desctiption: (描述)
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/user")
public class UserController {
    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserService userService;

    /**
     * 注册
     * @return String
     */
    @ApiOperation("注册(Security默认加密)")
    @PostMapping("/register")
    public ApiResousUtil<Boolean> register(@RequestBody UserDTO user){
        UserEntity userEntity = new UserEntity();
        userEntity.setRoles("ADMIN")
                .setEnable(true)
                .setUserName(user.getUserName())
                .setPassword(passwordEncoder.encode(user.getPassword()));
        userService.save(userEntity);
        return ApiResousUtil.ok(true);
    }
}
